package logic

import (
	"context"
	"time"
	"user/model/redis"

	"github.com/mojocn/base64Captcha"

	"user/userrpc/internal/svc"
	"user/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmaLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmaLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmaLogic {
	return &SendSmaLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmaLogic) SendSma(in *userrpc.SendSmaRequest) (*userrpc.SendSmaResponse, error) {
	// todo: add your logic here and delete this line

	c := base64Captcha.NewCaptcha(base64Captcha.DefaultDriverDigit, base64Captcha.DefaultMemStore)
	_, image, code, err := c.Generate()
	if err != nil {
		return nil, err
	}
	redis.Client.Set("captcha"+in.Form+in.Account, code, time.Minute*3)
	return &userrpc.SendSmaResponse{Image: image}, nil
}
